package com.spic.business.hidden.screenprogram.service.impl;

import com.spic.business.common.datascope.anotation.BusinessDataScope;
import com.spic.business.hidden.screenprogram.domain.HiddenScreenProgram;
import com.spic.business.hidden.screenprogram.domain.HiddenScreenProgramSubtable;
import com.spic.business.hidden.screenprogram.mapper.HiddenScreenProgramMapper;
import com.spic.business.hidden.screenprogram.service.IHiddenScreenProgramService;
import com.spic.business.two.tickets.general.eum.TicketTypesEnum;
import com.spic.business.two.tickets.general.service.TicketCodingService;
import com.spic.common.core.constant.SecurityConstants;
import com.spic.common.core.domain.R;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.SecurityUtils;
import com.spic.common.core.utils.StringUtils;
import com.spic.system.api.RemoteUserService;
import com.spic.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 隐患排查计划Service业务层处理
 *
 * @author yangjie
 * @date 2021-10-14
 */
@Service
public class HiddenScreenProgramServiceImpl implements IHiddenScreenProgramService
{
    @Autowired
    private HiddenScreenProgramMapper hiddenScreenProgramMapper;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private TicketCodingService ticketCodingService;
    /**
     * 查询隐患排查计划
     *
     * @param id 隐患排查计划主键
     * @return 隐患排查计划
     */
    @Override
    public HiddenScreenProgram selectHiddenScreenProgramById(Long id)
    {
        return hiddenScreenProgramMapper.selectHiddenScreenProgramById(id);
    }

    /**
     * 查询隐患排查计划列表
     *
     * @param hiddenScreenProgram 隐患排查计划
     * @return 隐患排查计划
     */
    @Override
    @BusinessDataScope(menuPath = "business/hidden/screen_program/index")
    public List<HiddenScreenProgram> selectHiddenScreenProgramList(HiddenScreenProgram hiddenScreenProgram)
    {
        return hiddenScreenProgramMapper.selectHiddenScreenProgramList(hiddenScreenProgram);
    }

    /**
     * 新增隐患排查计划
     *
     * @param hiddenScreenProgram 隐患排查计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHiddenScreenProgram(HiddenScreenProgram hiddenScreenProgram)
    {
        hiddenScreenProgram.setCreateTime(DateUtils.getNowDate());
        String username = SecurityUtils.getUsername();
        R<LoginUser> userInfo = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
        LoginUser data = userInfo.getData();
        String deptName = data.getSysUser().getDept().getDeptName();
        String sysCodeingResultByName = ticketCodingService.getSysCodeingResultByName(deptName, TicketTypesEnum.SUPERVISE_EXAMINE);
        hiddenScreenProgram.setSerialCode(sysCodeingResultByName);
        int rows = hiddenScreenProgramMapper.insertHiddenScreenProgram(hiddenScreenProgram);
        insertHiddenLibraryManagement(hiddenScreenProgram);
        return rows;
    }

    /**
     * 修改隐患排查计划
     *
     * @param hiddenScreenProgram 隐患排查计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateHiddenScreenProgram(HiddenScreenProgram hiddenScreenProgram)
    {
        hiddenScreenProgram.setUpdateTime(DateUtils.getNowDate());
        hiddenScreenProgramMapper.deleteHiddenLibraryManagementByParentId(hiddenScreenProgram.getId());
        insertHiddenLibraryManagement(hiddenScreenProgram);
        return hiddenScreenProgramMapper.updateHiddenScreenProgram(hiddenScreenProgram);
    }

    /**
     * 批量删除隐患排查计划
     *
     * @param ids 需要删除的隐患排查计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHiddenScreenProgramByIds(Long[] ids)
    {
        hiddenScreenProgramMapper.deleteHiddenLibraryManagementByParentIds(ids);
        return hiddenScreenProgramMapper.deleteHiddenScreenProgramByIds(ids);
    }

    /**
     * 删除隐患排查计划信息
     *
     * @param id 隐患排查计划主键
     * @return 结果
     */
    @Override
    public int deleteHiddenScreenProgramById(Long id)
    {
        hiddenScreenProgramMapper.deleteHiddenLibraryManagementByParentId(id);
        return hiddenScreenProgramMapper.deleteHiddenScreenProgramById(id);
    }

    @Override
    public List<Map<String, Object>> getLatest() {
        return hiddenScreenProgramMapper.getLatest();
    }

    /**
     * 新增排查清单库管理信息
     *
     * @param hiddenScreenProgram 隐患排查计划对象
     */
    public void insertHiddenLibraryManagement(HiddenScreenProgram hiddenScreenProgram)
    {
        List<HiddenScreenProgramSubtable> hiddenLibraryManagementList = hiddenScreenProgram.getHiddenLibraryManagementList();
        Long id = hiddenScreenProgram.getId();
        if (StringUtils.isNotNull(hiddenLibraryManagementList))
        {
            List<HiddenScreenProgramSubtable> list = new ArrayList<HiddenScreenProgramSubtable>();
            for (HiddenScreenProgramSubtable hiddenLibraryManagement : hiddenLibraryManagementList)
            {
                hiddenLibraryManagement.setParentId(id);
                hiddenLibraryManagement.setId(null);
                list.add(hiddenLibraryManagement);
            }
            if (list.size() > 0)
            {
                hiddenScreenProgramMapper.batchHiddenLibraryManagement(list);
            }
        }
    }
}
